using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Onca.Model.Exception;

namespace Onca.Model.Components
{
    class ComponentMemento : IComponentMemento
    {
        Hashtable componentMembers;

        ComponentMemento()
        {
            componentMembers = new Hashtable();
        }

        #region IComponentMemento Members

        public Object this[int index]
        {
            get
            {
                object foundedMember = null;

                if (index < 0 || index >= componentMembers.Count)
                {
                    throw new IndexOutOfRangeException("The index is out of range.");
                }
                else
                {
                    int current = 0;

                    foreach (object member in componentMembers)
                    {
                        if (current.Equals(index))
                        {
                            foundedMember = member;
                        }
                    }
                }

                return foundedMember;
            }
        }

        public Object this[string key]
        {
            get
            {
                object foundedMember;

                if (String.IsNullOrEmpty(key))
                {
                    throw new NullReferenceException("The key value is null or empty.");
                }
                else
                {
                    try
                    {
                        foundedMember = componentMembers[key];
                    }
                    catch (System.Exception e)
                    {
                        throw new KeyNotFoundException("The key doesn't match any value", e);
                    }
                }

                return foundedMember;
            }
        }

        public Object this[string key, object member]
        {
            set
            {
                if (componentMembers.Contains(key))
                {
                    throw new DuplicatedMemberKeyException();
                }
            }
        }

        public void Clear()
        {
            componentMembers.Clear();
        }

        #endregion
    }
}
